<template>
  <div class="login-container">
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- logo -->
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
      </div>
      <!-- 表单模块 -->
      <div class="login_form_bgc">
        <!-- 表单用户名 -->
        <el-form-item prop="loginName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.loginName"
            placeholder="请输入账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <!-- 表单密码 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="请输入验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          />
          <div class="login-code">
            <img :src="codeUrl + random" class="login-code-img" @click="getCode">
          </div>
        </el-form-item>
        <!-- 确认按钮 -->
        <el-button
          v-loading="loading"
          class="loginBtn"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
        >登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 用户输入的用户名，密码，验证码
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: ''
      },
      loginRules: {
        // 用户名
        loginName: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { pattern: 'admin', message: '账号格式不正确', trigger: 'blur' }
        ],
        // 密码
        password: [
          // min: 6, max: 16最小值与最大值
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: 'admin', message: '密码格式不正确', trigger: 'blur' }
          // { min: 5, max: 16, message: '密码必须在5到16位', trigger: 'blur' }
        ],
        // 验证码
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 3, max: 5, message: '密码必须在5到16位', trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      redirect: undefined,
      // 验证码图片地址
      codeUrl: 'http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/',
      random: 0,
      // 登录请求传的参数
      data: {
      },
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  // 初始化的时候调用一下点击验证码，生成一个随机数
  created() {
    this.getCode()
  },
  methods: {
    // 密码展示与隐藏，和获取焦点
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录执行的方法,表单的兜底校验
    async handleLogin() {
      // 登录接口的参数
      this.data = {
        // 用户名
        loginName: this.loginForm.loginName,
        // 密码
        password: this.loginForm.password,
        // mobile:,
        // 验证码
        code: this.loginForm.code,
        // 客户端请求的token-------随机数
        clientToken: String(this.random),
        // 账号
        loginType: 0
        // account:,
      }
      // 兜底校验是elementui封装好的方法
      this.$refs.loginRef.validate(async valid => {
        // valid是兜底验证，值为true或false
        // 验证成功才可得以进行登录
        if (valid) {
          try {
            this.loading = true
            // 调登录接口
            await this.$store.dispatch('user/SetTokenFn', this.data)
            // 路由跳转
            this.$router.push('/')
            this.loading = false
          } catch (err) {
            console.log(err)
            this.loading = false
          }
        } else {
          console.log('error')
          return false
        }
      })
    },
    // 点击的时候就图片生成验证码，更换图片
    getCode() {
      this.random = Math.floor(Math.random() * 1000)
    }
  }
}
</script>

<style lang="scss" >
// 全局生效的样式
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #ccc;
$cursor: #ccc;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      // background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ccc;
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__error {
    // color: #fff;
  }
}
</style>

<style lang="scss" scoped>
// 只对当前组件生效的样式
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("~@/assets/common/login.jpg"); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 0px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .login_form_bgc{
    background-color: #fff;
    padding: 76px 35px 0;
    border-radius: 10px;
    position: relative;
    top: -56px;
  }
  .loginBtn {
    background: #6b7cef;
    height: 64px;
    line-height: 32px;
    font-size: 24px;
    border-radius: 13px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    width: 96px;
    height: 96px;
    position: relative;
    margin: 0 auto;
    z-index: 1000;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 0px auto;
      text-align: center;
      font-weight: bold;
      position: absolute;
      img{
        width: 100%;
        height: 100%;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.login-code{
  width: 135px;
  height: 50px;
  float: right;
  img{
    width: 100%;
  }
}
</style>
